<?php

namespace app\index\model;

use think\Model;

class User extends Model
{



    public function address(){
        return $this->hasMany('Address');
    }

    public function comments(){
        return $this->hasMany('Comments','user_id','id');
    }

    public function login($data)
    {
        $user = db('user')->where('user_name', $data['username'])->find();
        if ($user) {
            $pwd = db('user')->where([
                'password' => md5($data['password']),
                'user_name' => $data['username']
            ])->find();
            if ($pwd&&$user) {
                //密码账户正确
                session('username', $data['username']);
                session('uid', $pwd['id']);

                return 0;
            } else {
                //密码错误
                return 2;
            }
        } else {
            //用户名不存在
            return 1;
        }
    }



}
